package com.smile.stream.group;

import com.smile.stream.StreamUtil;
import com.smile.stream.pojo.Dish;
import com.smile.stream.pojo.Type;

import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static java.util.stream.Collectors.*;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 21:39
 */
public class GroupDemo3 {
    public static void main(String[] args) {
        Map<Type, Long> typesCount = StreamUtil.getMenu()
                .stream()
                .collect(groupingBy(Dish::getType, counting()));
        System.out.println("typesCount = " + typesCount);
        /**
         * 输出结果
         * typesCount = {MEAT=3, OTHER=4, FISH=2}
         */

        Map<Type, Optional<Dish>> collect = StreamUtil.getMenu()
                .stream()
                .collect(groupingBy(Dish::getType,maxBy(Comparator.comparingInt(Dish::getCalories))));
        System.out.println("collect = " + collect);
        /**
         * 输出结果
         * collect = {MEAT=Optional[pork], OTHER=Optional[pizza], FISH=Optional[salmon]}
         */

        Map<Type, Dish> collect1 = StreamUtil.getMenu()
                .stream()
                .collect(groupingBy(Dish::getType,
                        collectingAndThen(maxBy(Comparator.comparingInt(Dish::getCalories)),Optional::get)));
        System.out.println("collect1 = " + collect1);
        /**
         * 输出结果
         * collect1 = {MEAT=pork, OTHER=pizza, FISH=salmon}
         */
        
    }
}
